package com.aaa.service.impl;

import com.aaa.entity.Roomtype;
import com.aaa.mapper.OrderdetailsMapper;
import com.aaa.mapper.RoomtypeMapper;
import com.aaa.service.RoomtypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoomtypeServiceImpl implements RoomtypeService {
    @Resource
    private RoomtypeMapper roomtypeMapper;
    @Resource
    private OrderdetailsMapper orderdetailsMapper;
    @Override
    public List<Map<String, Object>> findtype() {
        return roomtypeMapper.findtype();
    }

    @Override
    public PageInfo<Roomtype> roomfind(String typename,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Roomtype> list=roomtypeMapper.roomfind(typename);
        PageInfo<Roomtype> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Integer add(Roomtype roomType) {
        return roomtypeMapper.add(roomType);
    }

    @Override
    public List<Map<String, Object>> query(Integer id,String typename) {
        return roomtypeMapper.query(id,typename);
    }


    /*@Override
    public Integer delete(Integer id) {
        return roomtypeMapper.delete(id);
    }*/

    @Override
    public Roomtype toedit(Integer id) {
        return roomtypeMapper.toedit(id);
    }

    @Override
    public Integer update(Roomtype roomType) {
        return roomtypeMapper.update(roomType);
    }



    @Override
    public Object choicedate(String choiceEnddate, Integer rid, Integer odid) throws ParseException {
        //查询入住的房间单价和预计退房时间
        List<Map<String, Object>> list1=orderdetailsMapper.findcheckout(odid);
        long money=Integer.parseInt(list1.get(0).get("money").toString());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date enddate=sdf.parse(list1.get(0).get("checkout").toString());
        Date xudate=sdf.parse((choiceEnddate.replace("T"," ").substring(0,19)));
        //续房时间与原定结束日期的天数差
        System.out.println(enddate);
        System.out.println(xudate);
        long betweenDate=  (xudate.getTime()-enddate.getTime())/(1000*3600*24);
        System.out.println(betweenDate);
        money=money*betweenDate;
        System.out.println(money);
        //return roomtypeMapper.choicedate(choiceEnddate,rid);
        Map<String,Object> map=new HashMap<>();
        map.put("day",betweenDate);
        map.put("money",money);
        map.put("value3",choiceEnddate);
        return map;
    }
}
